/**
 *  Copyright(C) 2012-2013 Suntec(Shanghai) Software Co., Ltd.
 *  All Right Reserved.
 */
package com.cugbmao.luence;

import java.io.File;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

/**
 * Descriptions
 * 
 * @version 2013年5月21日
 * @author Suntec
 * @since JDK1.6
 * 
 */
public class IndexSearch {

	/**
	 * @param args
	 */
	public static void main( String[] args ) throws Exception {
		String filePath = "D:\\Lucene\\index4";

		Directory dir = FSDirectory.open( new File( filePath ) );

		IndexReader reader = DirectoryReader.open( dir );
		IndexSearcher searcher = new IndexSearcher( reader );
		Term term = new Term( "contents", "OpenGL" );
		TermQuery query = new TermQuery( term );
		TopDocs docs = searcher.search( query, 5 );
		ScoreDoc[] scoreDocs = docs.scoreDocs;
		System.out.println( "查询结果总数：" + docs.totalHits + "，最大的评分：" + docs.getMaxScore() );
		for ( int i = 0; i < scoreDocs.length; i++ ) {
			int doc = scoreDocs[i].doc;
			Document document = searcher.doc( doc );
			System.out.println( "filename=" + document.get( "filename" ) );
			System.out.println( "id=" + doc + "，scores=" + scoreDocs[i].score + "，index=" + scoreDocs[i].shardIndex );
		}
		reader.close();
	}
}
